<template>
  <Tab @goBack="goBack" :tabs="tabs" :current="current">
    <div slot="index">
      <el-form :inline="true" label-width="80px">
        <el-form-item label="客户端">
          <el-select @keyup.enter.native="handleQuery" v-model="queryParams['checkVersion.role']" placeholder="客户端"
                     clearable
                     size="small">
            <el-option
              v-for="dict in roleOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="系统类型">
          <el-select @keyup.enter.native="handleQuery" v-model="queryParams.system" placeholder="系统类型"
                     clearable
                     size="small">
            <el-option
              v-for="dict in systemOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="状态">
          <el-select
            @keyup.enter.native="handleQuery" v-model="queryParams['checkVersion.status']" placeholder="状态" clearable
            size="small">
            <el-option
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button type="primary" size="mini" icon="el-icon-plus" @click="handleInsert"
                     v-hasPermi="['main:banner:add']">添加
          </el-button>
        </el-form-item>


      </el-form>

      <el-table v-loading="loading" :data="postList">
        <el-table-column label="客户端" align="center" prop="checkVersion.role" :formatter="typeFormat"/>
        <el-table-column label="LOGO" align="center" prop="logourl">
          <template slot-scope="scope">
            <div>
              <el-popover placement="right" title="" trigger="click">
                <img :src="scope.row.logourl" alt="">
                <img slot="reference" :src="scope.row.logourl" alt="" style="max-height: 50px;max-width: 50px">
              </el-popover>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="系统类型" align="center" prop="system" :formatter="typeFormat"/>
        <el-table-column label="最新版本" align="center" prop="newversion"/>
        <el-table-column label="最低版本" align="center" prop="minversion"/>
        <el-table-column label="状态" align="center" prop="checkVersion.status" :formatter="typeFormat"/>
        <el-table-column label="是否强制" align="center" prop="isforce" :formatter="typeFormat"/>
        <el-table-column label="更新地址" align="center" prop="url" :show-tooltip-when-overflow="true">
          <template slot-scope="scope">
            <el-link :href="scope.row.url" target="_blank" type="primary">{{scope.row.url}}</el-link>
          </template>
        </el-table-column>
        <el-table-column label="电话" align="center" prop="mobile" :show-tooltip-when-overflow="true"/>
        <!--<el-table-column label="备注" align="center" prop="remark" :show-tooltip-when-overflow="true"/>-->
        <el-table-column label="创建时间" align="center" prop="createtime" :show-tooltip-when-overflow="true"/>
        <el-table-column label="操作" align="center" width="230">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              @click="handleOnline(scope.row)"
              v-hasPermi="['main:banner:edit']"
              v-show="scope.row.status===1"
            >上线
            </el-button>
            <el-button
              size="mini"
              @click="handleOffline(scope.row)"
              v-hasPermi="['main:banner:edit']"
              v-show="scope.row.status===2"
              type="warning"
            >下线
            </el-button>
            <el-button
              size="mini"
              type="success"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['main:banner:edit']"
              v-show="scope.row.status!==0"
            >编辑
            </el-button>
            <el-button
              size="mini"
              type="primary"
            >审核
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <div slot="add">
      <Add :areaList="areaList" :system-options="systemOptions" :role-options="roleOptions" :force-options="forceOptions" :row="row"
           @goBack="goBack"></Add>
    </div>
  </Tab>

</template>

<script>
  import Tab from "@/components/Tab";
  import {listVersion, editVersion, addVersion, removeVersion} from "@/api/main/app/version";
  import Add from "./add";

  export default {
    components: {Tab, Add},
    data() {
      return {
        row: undefined,
        open: false,
        addLoading: false,
        loading: false,
        daterange: undefined,
        areaList: undefined,
        cityList: [],
        postList: [],
        roleOptions: [],
        systemOptions: [],
        forceOptions: [],
        statusOptions: [
          {"dictLabel": "审核中", "dictValue": "0"}
          , {"dictLabel": "审核通过", "dictValue": "1"}
        ],
        current: 'index',
        tabs: [
          {index: 'index', name: ''}, {'index': 'add', name: '新增'}
        ],
        title: '',
        total: 0,
        loadingInstance: undefined,
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          system: undefined,
          'checkVersion.role':undefined,
          'checkVersion.status':undefined
        }
      }
    },
    created() {
      this.getDicts("yy_user_role").then(response => {
        this.roleOptions = response.data;
        console.log(this.roleOptions);
      });
      this.getDicts("yy_system_type").then(response => {
        this.systemOptions = response.data;
      });
      this.getDicts("yy_system_force").then(response => {
        this.forceOptions = response.data;
      });
      this.getList();
    },
    methods: {
      getList() {
        this.loading = true;
        listVersion(this.queryDateRange(this.queryParams, this.daterange)).then(response => {
          console.log(response);
          this.postList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      handleInsert() {
        this.row = undefined;
        this.$set(this.tabs, 1, {index: 'add', name: '新增版本'});
        this.current = 'add';
      },
      handleUpdate(row) {
        this.row = row;
        this.$set(this.tabs, 1, {index: 'add', name: '编辑版本'});
        this.current = 'add';
      },
      handleOffline(row) {
        let _this = this;
        this.$confirm(`确定要更改标题名为[${row.title}]的状态为下线?`, "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "info"
        }).then(function () {
          editVersion({id: row.id, status: 0, cityId: row.cityId}).then(response => {
            _this.msgSuccess("下线成功");
            _this.getList();
          });
        }).catch(function (e) {
          console.log(e);
        });
      },
      handleOnline(row) {
        let _this = this;
        this.$confirm(`确定要更改标题名为[${row.title}]的状态为上线?`, "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "info"
        }).then(function () {
          editVersion({id: row.id, status: 2, cityId: row.cityId}).then(response => {
            _this.msgSuccess("上线成功");
            _this.getList();
          });
        }).catch(function (e) {
          console.log(e);
        });
      },
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      cancel() {
        this.open = false;
        this.reset();
      },
      typeFormat(row, column) {
        let option;
        let columnVal;
        if (column.property === 'system') {
          option = this.systemOptions;
          columnVal = row[column.property];
        } else if (column.property === 'checkVersion.role') {
          option = this.roleOptions;
          columnVal = row.checkVersion.role;
        } else if (column.property === 'isforce') {
          option = this.forceOptions;
          columnVal = row[column.property];
        } else if (column.property === 'checkVersion.status') {
          option = this.statusOptions;
          columnVal = row.checkVersion.status
        }
        return this.selectDictLabel(option, columnVal);
      },
      goBack(message) {
        this.current = 'index';
        if (message === 'addBanner' || message === 'editBanner') {
          this.getList();
        }
      }
    }
  }
</script>

<style>

</style>
